const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const inquirer = require("inquirer");

// 应用问题
const questions = [
  {
    type: "confirm",
    name: "isCover",
    message: "当前应用名称已存在，是否要覆盖？",
    default: "",
  },
];

const isAppExsit = async (appName = "") => {
  if (!appName) {
    const emptyTip = "应用名称不能为空";
    console.log(chalk.red.bold(emptyTip));
    return Promise.reject(emptyTip);
  }

  /**
   * process.cwd() 为命令行执行目录
   * appUrl 应用地址
   * isExsit 当前应用是否存在
   */
  const appUrl = path.join(process.cwd(), appName);
  const isExsit = fs.existsSync(appUrl);

  // 应用存在时判断是否要覆盖
  if (isExsit) {
    const coverTip = "请手动删除同名文件夹才能下载当前模板";
    return Promise.reject(coverTip);
  } else {
    return Promise.resolve(true);
  }
};

module.exports = {
  isAppExsit,
};
